package com.aspsine.multithreaddownload.db;

import com.aspsine.multithreaddownload.util.L;
import ohos.app.Context;
import ohos.data.rdb.RdbStore;
import ohos.data.resultset.*;
import java.util.ArrayList;
import java.util.List;

import static com.aspsine.multithreaddownload.util.Const.TABLE_NAME;

/**
 * Created by aspsine on 15-4-19.
 */
public class ThreadInfoDao extends AbstractDao<ThreadInfo> {

    private static final String TAG = "ThreadInfoDao ";

    public ThreadInfoDao(Context context) {
        super(context);
    }

    public static void createTable(RdbStore db) {
        db.executeSql("create table if not exists " +
                TABLE_NAME + "(_id integer primary key autoincrement, id integer, " +
                "tag text, uri text, start long, end long, finished long)");
    }

    public static void dropTable(RdbStore db) {
        db.executeSql("drop table if exists " + TABLE_NAME);
    }

    public void insert(ThreadInfo info) {
        RdbStore db = getWritableDatabase();
        db.executeSql("insert into "
                        + TABLE_NAME
                        + "(id, tag, uri, start, end, finished) values(?, ?, ?, ?, ?, ?)",
                new Object[]{info.getId(), info.getTag(), info.getUri(),
                        info.getStart(), info.getEnd(), info.getFinished()});
    }

    public void delete(String tag) {
        RdbStore db = getWritableDatabase();
        db.executeSql("delete from "
                        + TABLE_NAME
                        + " where tag = ?",
                new Object[]{tag});
    }

    public void update(String tag, int threadId, long finished) {
        RdbStore db = getWritableDatabase();
        db.executeSql("update "
                        + TABLE_NAME
                        + " set finished = ?"
                        + " where tag = ? and id = ? ",
                new Object[]{finished, tag, threadId});
    }

    public List<ThreadInfo> getThreadInfos(String tag) {
        List<ThreadInfo> list = new ArrayList<ThreadInfo>();
        RdbStore db = getReadableDatabase();

        ResultSet result = db.querySql("select * from "
                        + TABLE_NAME
                        + " where tag = ?",
                new String[]{tag});
        while (result.goToNextRow()){
            ThreadInfo info = new ThreadInfo();
            info.setId(result.getInt(result.getColumnIndexForName("id")));
            info.setTag(result.getString(result.getColumnIndexForName("tag")));
            info.setUri(result.getString(result.getColumnIndexForName("uri")));
            info.setEnd(result.getLong(result.getColumnIndexForName("end")));
            info.setStart(result.getLong(result.getColumnIndexForName("start")));
            info.setFinished(result.getLong(result.getColumnIndexForName("finished")));
            list.add(info);
        }
        result.close();
        return list;
    }

    public List<ThreadInfo> getThreadInfos() {
        List<ThreadInfo> list = new ArrayList<ThreadInfo>();
        RdbStore db = getReadableDatabase();
        ResultSet result = db.querySql("select * from " + TABLE_NAME, null);
        while (result.goToNextRow()){
            ThreadInfo info = new ThreadInfo();
            info.setId(result.getInt(result.getColumnIndexForName("id")));
            info.setTag(result.getString(result.getColumnIndexForName("tag")));
            info.setUri(result.getString(result.getColumnIndexForName("uri")));
            info.setEnd(result.getLong(result.getColumnIndexForName("end")));
            info.setStart(result.getLong(result.getColumnIndexForName("start")));
            info.setFinished(result.getLong(result.getColumnIndexForName("finished")));
            list.add(info);
        }
        result.close();
        return list;
    }

    public boolean exists(String tag, int threadId) {
        RdbStore db = getReadableDatabase();
        ResultSet result = db.querySql("select * from "
                        + TABLE_NAME
                        + " where tag = ? and id = ?",
                new String[]{tag, threadId + ""});
        boolean isExists = result.goToNextRow();//cursor.moveToNext();
        result.close();
        return isExists;
    }

}
